//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "UIView+Frame.h"

@interface AppDelegate () <UITextFieldDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 程序的组成结构
    // 1. main.m (没有他, 程序不能正常运行)
    // 2. AppDelegate (程序的执行者 签订了UIApplicationDelegate协议 其中的所有协议方法都是可选的) AppDelegate就和一个普通的类 因为签订了协议 所以得到很多方法
    // 3. ViewController(主要负责视图管理)
    // 4. Main.storyboard (主要负责视图管理) LaunchScreen.storyboard (可视化管理 主要负责启动页)
    // 5. Assets.xcassets (主要用来管理图片素材) 原来叫 image.xcassets(x-code 7 之前)
    // 6. LaunchScreen.storyboard
    // 7. info.plist (工程配置文件)
    // 模拟器 command + 1 ~ 5 (改变模拟器大小)
    // command + ← / → 横屏 / 竖屏
    
    // iOS
    // 1. UIWindow (程序中只有一个主Window)
    //   1> 取消程序的可视化入口 Main.sb
    //   2> 创建主Window对象 (使其大小与主屏幕一样)
    // UIScreen 系统屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 给Window 添加颜色
    self.window.backgroundColor = [UIColor whiteColor];
    // 让Window 显示并成为主窗口
    [_window makeKeyAndVisible];
    // 从x-code7以后 必须设置 rootViewController
    // 设置Windows主窗口
    _window.rootViewController = [[ViewController alloc] init];
    // UIWindow继承于UIView 与NSObject一样 大部分UI控件继承于UIView
#if 0
    // 设置坐标
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 270)];
    view.backgroundColor = [UIColor cyanColor];
    // 将view添加到window
    [self.window addSubview:view];
    // boinds 对视图的影响测试
    // CGRect CGPoint
    NSLog(@"%@", NSStringFromCGRect(view.frame));
    NSLog(@"改变前的:%@", NSStringFromCGPoint(view.center));
    view.bounds = CGRectMake(0, 0, 100, 100);
    NSLog(@"%@", NSStringFromCGRect(view.frame));
    NSLog(@"改变后的:%@", NSStringFromCGPoint(view.center));
    // iOS坐标系 与 数学上的坐标系不同 y轴正方向是想下的 原点是屏幕的左上角
    // frame bounds center 都是UIView的属性
    // 1. frame (x, y, width, height) 指的是在他父视图上的位置
    // 2. bounds (x, y, width, height) 指的是视图本身的坐标系 bounds x y 修改了本身的坐标 影响他的子视图 bounds (x, y) 修改的是本身的大小 根据其中心点进行收缩或扩张 bounds的改变 不会影响其中心点
    
    // 3. center 中心点
    // view.frame = CGRectMake(0, 0, 100, 100);
    // view.bounds = CGRectMake(10, 10, 380, 80); // 改变x y 是改变了其子视图的坐标
    // UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // aView.backgroundColor = [UIColor purpleColor];
    // [view addSubview:aView];

    // 改变view的大小
    CGRect rect = view.frame;
    rect.size.width = 100; // 结构体
    view.frame = rect;
    // view.frame 是getter方法 frame.size 是结构体 OC和C的内容结合 是一个常量 不能赋值
    // view.frame.size.width = 100;
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 200, view.frame.size.height);
    
    // UIView的基本属性
    UIView *testView = [[UIView alloc] init];
    
    [testView setYh_x:0];
    [testView setYh_y:0];
    [testView setYh_width:100];
    [testView setYh_height:100];
    
    // testView.backgroundColor = [UIColor greenColor];
    [_window addSubview:testView];
    // [testView removeFromSuperview];
    // alpha 透明度 0.0 ~ 1.0 默认为1.0
    testView.alpha = 0.4;
    // tag 标记 编号 默认为0 根据tag获取view
    testView.tag = 1000;
    UIView *view1 = [_window viewWithTag:1000];
    // subViews
    UIView *arrayView = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", arrayView);
    // superView
    // testView.superview;  window
    // hiden 隐藏 / 显示 默认"NO"
    testView.hidden = YES;
    testView.hidden = NO;

    // 视图层级
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    view1.backgroundColor = [UIColor blueColor];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
    view2.backgroundColor = [UIColor blackColor];
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    view3.backgroundColor = [UIColor orangeColor];
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
    view4.backgroundColor = [UIColor greenColor];
    UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(40, 40, 200, 200)];
    view5.backgroundColor = [UIColor grayColor];
    // addSubview
    [_window addSubview:view1];
    // insertSubview: atIndex:
    [_window insertSubview:view2 atIndex:2];
    [_window insertSubview:view3 atIndex:3];
    [_window insertSubview:view4 atIndex:4];
    [_window insertSubview:view5 atIndex:1];
#endif

//    [_window insertSubview:view4 atIndex:1];
//    [_window insertSubview:view5 atIndex:0];

    // insertSubview: aboveSubview:
//    [_window insertSubview:view3 aboveSubview:view1];
    // insertSubview: belowSubview:
//    [_window insertSubview:view2 belowSubview:view1];
#if 0
    // UILabel
    // 使用父类初始化方法
    // 用来显示文本
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 270, 120)];
    label.backgroundColor = [UIColor blackColor];
    [_window addSubview:label];
    // UILabel的属性
    // text
    label.text = @"美梦是个气球 牵在手上 向往蓝天 不管高低 不曾远离 我视线 梦想是个诺言 写在心上 明天你是否会想起 昨天你写的日记 明天你是否还惦记曾经最爱哭的你 老师们都已想不起";
    // textColor
    label.textColor = [UIColor whiteColor];
    // font
    label.font = [UIFont systemFontOfSize:17]; // 默认17
    // label.font = [UIFont boldSystemFontOfSize:10];  加粗
    // textAlignment 对齐方式 0 1 2 枚举
    label.textAlignment = 0;
    // numberOfLines 不确定行数时 赋值 0
    label.numberOfLines = 0;
    // UIButton
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 60, 30);
    button.backgroundColor = [UIColor redColor];
    [_window addSubview:button];
    // 给button添加事件
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    // 添加文字
    [button setTitle:@"确认" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    // UITextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 370, 270, 30)];
    textField.backgroundColor = [UIColor redColor];
    [_window addSubview:textField];
    // 给输入框赋初值
//    textField.text = @"文字";
    // 文字颜色
    textField.textColor = [UIColor blackColor];
    // borderStyle 边缘
    textField.placeholder = @"请输入密码";
    textField.clearsOnBeginEditing = YES; // command + k
    
    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    imageButton.frame = CGRectMake(100, 200, 200, 30);
    imageButton.backgroundColor = [UIColor redColor];
    [self.window addSubview:imageButton];
    [imageButton setTitle:@"登录" forState:UIControlStateNormal];
    [imageButton setImage:[UIImage imageNamed:@"icon_password_select@2x.png"] forState:UIControlStateNormal];
    [imageButton setBackgroundImage:[UIImage imageNamed:@"welcome_2.jpg"] forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 30, 200, 30)];
    label.backgroundColor = [UIColor redColor];
    label.text = @"I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man.I am a nice man I want to be a perfect man";
    label.numberOfLines = 0;
    [label sizeToFit];
    label.lineBreakMode = 0; // 黄航模式 省略号的位置
    [self.window addSubview:label];
    label.shadowColor = [UIColor blackColor]; // 设置阴影
    label.shadowOffset = CGSizeMake(2, 2); // 设置阴影大小
#endif
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 270, 30)];
    textField.backgroundColor = [UIColor redColor];
    [self.window addSubview:textField];
    textField.delegate = self; // textField协议
    
    
    
    
    
    return YES;
}

- (void)click:(UIButton *)button {
    NSLog(@"点击");
    [_window endEditing:YES]; // 取消编辑
}

// 是否可以进行输入
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
// 已经编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
// 判断是否
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 回收键盘
    [textField endEditing:YES];
    return YES; // 处理惦记return需要进行的操作
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
